package com.sangmo.fts;

import com.sangmo.boot.framework.cache.CacheAspect;
import com.sangmo.boot.framework.cache.ICache;
import com.sangmo.boot.framework.cache.local.LocalCache;
import com.sangmo.boot.framework.common.session.UserSession;
import com.sangmo.boot.framework.plugin.redis.cache.RedisCache;
import com.sangmo.boot.framework.plugin.redis.client.IRedisClient;
import com.sangmo.boot.framework.plugin.redis.client.impl.DefaultRedisClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

import java.net.UnknownHostException;

@Configuration
public class CacheConfig {

    @Value("${spring.redis.clearOnStartup}")
    private boolean clearCacheOnStartup;

    /*@Bean
    public IRedisClient redisClient(RedisTemplate redisTemplate) {
        if (clearCacheOnStartup) {
            redisTemplate.delete("*");
        }
        DefaultRedisClient redisClient = new DefaultRedisClient();
        return redisClient;
    }

    @Bean
    public ICache redisCache() {
        return new RedisCache() {
            @Override
            public String makeCacheId(String sector, String key, Object query) {
                if (null != UserSession.getAuthorize()) {
                    sector = UserSession.getAuthorize().getToken() + "/" + sector;
                }
                return super.makeCacheId(sector, key, query);
            }
        };
    }*/

    @Bean
    @Primary
    public ICache localCache() {
        LocalCache cache = new LocalCache("/ehcache/ehcache.xml") {
            @Override
            public String makeCacheId(String sector, String key, Object query) {
                if (null != UserSession.getAuthorize()) {
                    sector = UserSession.getAuthorize().getToken() + "/" + sector;
                }
                return super.makeCacheId(sector, key, query);
            }
        };
        if (clearCacheOnStartup) {
            cache.expiredAll();
        }
        return cache;
    }


    @Bean
    public CacheAspect cacheAdvise(ICache cache) {
        CacheAspect advise = new CacheAspect();
        advise.setCache(cache);
        return advise;
    }

    /*@Bean
    public RedisTemplate<Object, Object> redisTemplate(
            RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
        template.setConnectionFactory(redisConnectionFactory);
//		Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
        template.setDefaultSerializer(serializer);
        return template;
    }*/

}
